package com.dh.web.controller;


import com.dh.core.security.RoleConstant;
import com.dh.core.util.BeanValueUtil;
import com.dh.entity.Notice;
import com.dh.entity.Notice;
import com.dh.service.NoticeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/admin/notice")
@Controller
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    /**
     * 转到列表展示页
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresUser
    public String show(Model model, @RequestParam(required = false, value = "page", defaultValue = "1") int page) {

        Page<Notice> pagination =
            noticeService.page(new PageRequest(page - 1, 100, Sort.Direction.ASC, "id"));
        System.out.println(pagination.getTotalPages());
        model.addAttribute("pagination", pagination);
        return "admin/notice/notice_list";
    }

    /**
     * 转到添加页面
     *
     * @return
     */
    @RequestMapping("/new")
    @RequiresUser
    public String editNew() {
        return "admin/notice/notice_create";
    }



    /**
     * 添加公告
     *
     * @param notice
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @RequiresUser
    public String create(Notice notice, RedirectAttributes redirectAttributes) {

        try {
            noticeService.create(notice);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "错误");
            return "redirect:/admin/notice/new";
        }
        return "redirect:/admin/notice/list";
    }

    /**
     * 删除一个公告
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<>();
        noticeService.delete(id);
        map.put("success", true);
        return map;
    }


    /**
     * 转到编辑公告页
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    @RequiresUser
    public String edit(@PathVariable Long id, Model model) {
        Notice notice = noticeService.get(id);
        model.addAttribute("notice", notice);
        return "admin/notice/notice_update";
    }

    /**
     * 转到显示公告页
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
    @RequiresUser
    public String show(@PathVariable Long id, Model model) {
        Notice notice = noticeService.get(id);
        model.addAttribute("notice", notice);
        return "admin/notice/notice_show";
    }


    /**
     * 更新公告
     *
     * @param id
     * @param notice
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @RequiresRoles(value = {RoleConstant.ROLE_ADMIN})
    public String update(@PathVariable("id") long id, Notice notice, RedirectAttributes redirectAttributes) {

        try {
            Notice p = noticeService.get(id);
            BeanValueUtil.copyProperties(notice,p);
            p.setLastModifyTime(new Date());
            noticeService.update(p);
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message", "错误");
            return "redirect:/admin/notice/edit/" + id;
        }
        return "redirect:/admin/notice/list";
    }

}
